package com.awspaas.user.apps.processforsap.impl.send;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.util.ArrayList;
import java.util.List;

public class SenderMM027Impl extends ParentInterface {

    public SenderMM027Impl(JSONObject jsonObject) {
        super(jsonObject);
//        this.dueInterFaceData();
    }

    @Override
    public void dueInterFaceData() {
        String result = "";
        String resultType = "S";
        try {
            String rdsId = SDK.getRuleAPI().executeAtScript("@getSapParamas(SAP_MID_ID)");
            RDSAPI api = SDK.getCCAPI().getRDSAPI(rdsId);
            MessageHeader messageHeader = super.messageHeader;
            JSONObject sendData = super.sendData;
            /**
             * 获取主表数据
             */
            String bindid = messageHeader.getUUID();
            BO mainBO = SDK.getBOAPI().query("BO_EU_SAPPR_APPLY").bindId(bindid).detail();
            JSONArray jsonArray = sendData.getJSONArray("Requests");
            JSONArray newJSONArray = new JSONArray();
            String kostl= mainBO.getString("KOSTL");//FS
            String werks= mainBO.getString("WERKS");//FS00
            String sql = "SELECT VALUE,TEXT FROM SAP_DICTIONARY WHERE TYPE='成本中心' AND TEXT='"+kostl+"' AND (( '"+werks+"' = 'FH00' AND VALUE LIKE 'FE%' ) OR ('"+werks+"' != 'FH00' AND VALUE LIKE 'FS%'))";

            RowMap rowMap = api.getMap(sql);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                jsonObject.put("BSART", mainBO.getString("BSART"));
                jsonObject.put("PREQ_NAME", mainBO.getString("AFNAM"));
                jsonObject.put("EKORG", mainBO.getString("EKORG"));
                jsonObject.put("EKGRP", mainBO.getString("EKGRP"));
                jsonObject.put("KOSTL", rowMap.getString("VALUE"));
                newJSONArray.add(jsonObject);
            }

            sendData.put("Requests", newJSONArray);

            BO configBo = messageHeader.getConfigBo();
            String url = configBo.getString("SEND_URL");
            result = HttpClientUtil.doPost(url, sendData.toJSONString(), SDK.getRuleAPI().executeAtScript("@getSapParamas(PONAME)"), SDK.getRuleAPI().executeAtScript("@getSapParamas(POPASSWORD)"));

            JSONObject returnJSON = JSONObject.parseObject(result);
            JSONObject newJsonResult =new JSONObject();

            JSONArray jsonArrayResponse = returnJSON.getJSONArray("Responses");
            List<JSONObject> newList = new ArrayList<>();
            for (int i = 0; i < jsonArrayResponse.size(); i++) {
                JSONObject jsonObject = jsonArrayResponse.getJSONObject(i);
                if (jsonObject.getString("E_RCODE").equals("S")) {
                    mainBO.set("BANFN", jsonObject.getString("BANFN"));
                    SDK.getBOAPI().update("BO_EU_SAPPR_APPLY", mainBO);
                } else {
                    resultType = "E";
                }
                newList.add(jsonObject);
            }
            //封装最终数据
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("Type", resultType);
            jsonObject1.put("MESSAGE", newList);
            newJsonResult.put("Responses", jsonObject1);
            result = JSONObject.toJSONString(newJsonResult);
            //处理序号
            List<BO> list = SDK.getBOAPI().query("BO_EU_SAPPR_PROLIST").bindId(bindid).orderBy("CREATEDATE").desc().list();
            for (int i=0;i<list.size();i++){
                BO bo = list.get(i);
                bo.set("HH",(i+1));
                bo.set("PRNO",mainBO.getString("BANFN"));
                SDK.getBOAPI().update("BO_EU_SAPPR_PROLIST",bo);
            }


        } catch (Exception e) {
            e.printStackTrace();
            super.resultType = false;
            throw new BPMNError("0331", e.getMessage());
        } finally {
            super.resultSendData = sendData.toJSONString();
            super.returnData = result;
        }
    }

}